package cn.tedu.oa.sys.service.impl;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import cn.tedu.oa.common.exception.ServiceException;
import cn.tedu.oa.common.service.impl.BaseServiceImpl;
import cn.tedu.oa.common.util.ShiroUtils;
import cn.tedu.oa.sys.dao.RoleMenuDao;
import cn.tedu.oa.sys.dao.UserRoleDao;
import cn.tedu.oa.sys.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import cn.tedu.oa.sys.dao.MenuDao;
import cn.tedu.oa.sys.entity.Menu;
import cn.tedu.oa.sys.service.MenuService;
import org.springframework.util.StringUtils;

@Service
public class MenuServiceImpl extends BaseServiceImpl<Menu> implements MenuService {

	@Autowired
	private MenuDao menuDao;

	@Autowired
	private UserRoleDao userRoleDao;

	@Autowired
	private RoleMenuDao roleMenuDao;

	@Override
	public List<Menu> getAll() {
		List<Menu> menus=menuDao.getAll();
		if (menus == null||menus.size()==0) {
			throw new ServiceException("菜单信息为空");
		}
		//根据type判断菜单类型，默认为菜单
		for (Menu menu:menus) {
			Integer type=menu.getType();
			if (type==null||type==0){
				menu.setTypeName("菜单");
			}else if (type==1){
				menu.setTypeName("按钮");
			}
		}
		return menus;
	}

	@Override
	public void add(Menu menu) {
		if (menu == null) {
			throw new ServiceException("添加失败：菜单信息为空");
		}
		if (StringUtils.isEmpty(menu.getName().trim())) {
			throw new ServiceException("添加失败：菜单名为空");
		}
		if (StringUtils.isEmpty(menu.getParentId())) {
			throw new ServiceException("添加失败：上级菜单为空");
		}
		if (StringUtils.isEmpty(menu.getPermission())){
			throw new ServiceException("添加失败：权限信息为空");
		}
		menu.setSort(5);
		menu.setAddTime(new Date());
		menu.setUpdateTime(new Date());
		menu.setAddUser(ShiroUtils.getPrincipal().getName());
		menu.setIsdel(0);
		menuDao.add(menu);
	}

	@Override
	public void update(Menu menu) {
		if (menu == null) {
			throw new ServiceException("编辑失败：传递的数据为空");
		}
		if (menuDao.get(menu.getId())==null) {
			throw new ServiceException("编辑失败：要编辑的菜单可能已被删除，请刷新确认");
		}
		menuDao.update(menu);
	}

	@Override
	public void delete(Serializable id) {
		if(id==null||(Integer)id<1)throw new ServiceException("删除失败：菜单ID为空");
		//查询是否存在子菜单
		List<Menu> menus=menuDao.getSubMenusByPreviousMenuId((Integer)id);
		if (menus.size() != 0) {
			throw new ServiceException("删除失败：请先删除子菜单或使用‘多项删除’");
		}
		roleMenuDao.deleteByMenuId((Integer) id);
		menuDao.delete(id);
	}

	@Override
	public void deleteByIds(Integer... ids){
		if(ids==null)throw new ServiceException("删除失败：菜单ID为空");
		roleMenuDao.deleteByMenuIds(ids);
		menuDao.deleteByIds(ids);
	}

	@Override
	public Menu get(Serializable id) {
		if(id==null)throw new ServiceException("查询失败：菜单ID为空");
		return menuDao.get(id);
	}

	@Override
	public List<Menu> getMenuPagerList(String criteria, Integer pageNo, Integer pageSize) {
		if (pageNo == null || pageNo < 1 || pageSize == null || pageSize < 1) {
			throw new IllegalArgumentException("参数不合法，请联系运维人员");
		}
		/*假设pageSize=10*/
		/*1 (1-1) x 10 + 1 = 1*/
		/*2 (2-1) x 10 + 1 = 11*/
		/*3 (3-1) x 10 + 1 = 21*/ /*@ming*/
		int startIndex = (pageNo - 1) * pageSize + 1;
		/*1 1 + 10 - 1 = 10*/
		/*2 11 + 10 - 1 = 20*/
		/*3 21 + 10 - 1 = 30*/ /*@ming*/
		int endIndex = startIndex + pageSize - 1;
		List<Menu> list = menuDao.getMenuPagerList(criteria, startIndex, endIndex);
		if (list == null) {
			throw new ServiceException("没有查询到数据");
		}
		return list;
	}

	//基于名字查询一级菜单信息
	@Override
	public Menu getMenuByName(String name) {
		if(name==null){
			throw new IllegalArgumentException("参数不合法，请联系运维人员");
		}
		Menu menu = menuDao.getMenuByName(name);
		if (menu==null){
			throw new IllegalArgumentException("参数不合法，请联系运维人员");
		}
		return menu;
	}
	//基于上级菜单查询子菜单(一级)
	@Override
	public List<Menu> getSubMenusByPreviousMenuId(Integer id) {
		System.out.println(id);
		if(id==null||id<1){
			throw new IllegalArgumentException("参数不合法，请联系运维人员");
		}
		List<Menu> list = menuDao.getSubMenusByPreviousMenuId(id);
		if(list==null){
			throw new ServiceException("没有更多数据");
		}
		return list;
	}

	//查询用户菜单权限


	@Override
	public List<Menu> getMenusByUser(Integer previousMenuId) {
		User user = ShiroUtils.getPrincipal();
		if(user==null){
			throw new ServiceException("系统故障，请联系运维人员");
		}
		List<Integer> roleIds=userRoleDao.getRoleIdsByUserId(user.getId());
		if(roleIds==null||roleIds.isEmpty()){
			return null;
		}
		List<Integer> menuIds=roleMenuDao.getMenuIdsByRoleIds(roleIds.toArray(new  Integer[]{}));
		if(menuIds==null||menuIds.isEmpty()){
			return null;
		}
		List<Menu> menus = menuDao.getMenus(previousMenuId,menuIds.toArray(new  Integer[]{}));
		if (menus==null||menus.isEmpty()){
			return null;
		}
		return menus;
	}

	//查询所有一级菜单
	@Override
	public List<Menu> getFirstMenus() {
		User user = ShiroUtils.getPrincipal();
		if(user==null){
			throw new ServiceException("系统故障，请联系运维人员");
		}
		List<Integer> roleIds=userRoleDao.getRoleIdsByUserId(user.getId());
		if(roleIds==null||roleIds.isEmpty()){
			return null;
		}
		List<Integer> menuIds=roleMenuDao.getMenuIdsByRoleIds(roleIds.toArray(new  Integer[]{}));
		if(menuIds==null||menuIds.isEmpty()){
			return null;
		}
		List<Menu> menus = menuDao.getFirstMenus(menuIds.toArray(new  Integer[]{}));
		if (menus==null||menus.isEmpty()){
			return null;
		}
		return menus;

	}
}
